﻿using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Threading;
using System.Xml.Serialization;

using SolverTemplateLib.BackTracker;

namespace Katamino
{
    class KataminoController
    {
        public static EventWaitHandle controllerRunning = new AutoResetEvent(true);
        public static void Calculate(KataminoForm form, DoWorkEventArgs e)
        {
            bw = form.KataminoBackgroundWorker();
            Bitmap bm = new Bitmap(form.ImageSize().Width, form.ImageSize().Height);
            Graphics gr = Graphics.FromImage(bm);

            KataminoBacktracker kt = new KataminoBacktracker(form, form.BoardWidth, form.BoardHeight);
            // KataminoBacktracker kt = new KataminoBacktracker(form, 5, 12);

            FileStream fs = new FileStream("Solutions.xml", FileMode.Create);
            XmlSerializer xs = new XmlSerializer(typeof(KataminoBacktracker));

            // it is required to run kt.Solve as an enumerator
            foreach (Backtracker b in kt.Solve())
            {
                xs.Serialize(fs, b);
                fs.Flush();
            }
            // ??? finish: close serialization streams
            fs.Close();
            kt.Finish();
        }
        private static BackgroundWorker bw;
    }
}
